<?php

include_once('dao.connection.php');
include_once('entity.linestring.php');
include_once('entity.point.php');

class LineStringDAO{

    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance))
            self::$instance = new LineStringDAO();

        return self::$instance;
    }

    private function __construct() {
        $this->connection = Connection::getInstance();
    }

    public function findById($id) {

        $sql = 'SELECT nm_linestring, x(AsText((ST_DumpPoints(linestring_geom)).geom)), Y(AsText((ST_DumpPoints(linestring_geom)).geom))
                FROM linestring
                WHERE id_linestring=' . $id;
        $result = $this->connection->executeSQL($sql);

        $linestring = new LineString();

        while ($row = pg_fetch_object($result)) {
            
            $linestring->setName($row->nm_linestring);
            $linestring->setId($id);
            
            $point = new Point();
            $point->setX($row->x);
            $point->setY($row->y);
            
            $points[] = $point;            
        }
        $linestring->setPoints($points);
        
        return $linestring;
    }

    public function listLinestrings() {

        $sql = 'SELECT * FROM linestring';
        $result = $this->connection->executeSQL($sql);

        while (list($id, $name) = pg_fetch_row($result)) {
            $linestring = new Linestring();
            $linestring->setId($id);
            $linestring->setName($name);

            $listaLinestrings[] = $linestring;
        }
        return $listaLinestrings;
    }

}

?>
